<?php
class Zend_View_Helper_TableViewPaginator extends Zend_View_Helper_Abstract
{
	protected $_view;
	public function setView(Zend_View_Interface $view)
	{
		$this->_view = $view;
	}		
	
	/**
	 * 
	 * Render the listView for a table with the given elements to show
	 * @param array $elementsToUse
	 * @param string $typeOfArray
	 * @param string $linkEdit
	 * @param array $showPaginator
	 */
	public function tableViewPaginator($resultData, $tableName, $linkEdit = null, $showPaginator = true) {
		if (empty($resultData)) {
			return '<h2>NO DATA</h2>';
		} else {
			$_htmlTableAdmin='';
			$_htmlTableAdmin = '<table class="dataAdmin'. ' ' . $tableName .'">
								<thead>
									<tr>' . $this->showHeader($resultData) . '</tr>
								</thead>
								<tbody>' . $this->showBody($resultData, $linkEdit) . '</tbody>
							</table>';
			if($showPaginator) {
				$_htmlTableAdmin .= $this->_view->paginationControl($resultData,
				'Sliding',
				'/pagination/pagination_control.phtml');
			}
			
			return $_htmlTableAdmin;
		}
	}
	
	protected function showHeader($headerToShow){
		$outputHeader = '';
		foreach($headerToShow as $headerData) {
			
			if(is_array($headerData)) {
				$outputHeader .= '<tr>';
				foreach ($headerData as $key => $value){
					if($key == "id") {
						$outputHeader .= '<th class="table-edit" scope="col"></th><th class="table-delete" scope="col"></th>';
					} else {
						$outputHeader .= '<th class="' . $key . '" scope="col">'. strtoupper(str_replace('_', ' ', $key)) .'</th>';
					}
				}
				$outputHeader .= '</tr>';
				break;
			} else {
				$outputHeader .= '<tr><th>' . strtoupper(str_replace('_', ' ', $headerData)) . '<th></tr>';
				break;
			}
			
        }
		 
       return $outputHeader;
	}
	
	protected function showBody($bodyToShow, $linkEdit)
	{
		$outputBody = '';
		foreach($bodyToShow as $bodData) {
			if(is_array($bodData)) {
				$outputBody .= '<tr>';
				foreach($bodData as $key => $value) {
						if($key == "id" && !empty($linkEdit)) {
							$outputBody .= '<td><a href="/admin/save'. $linkEdit . '/id/' . $value .'" title="editar"><img src="/images/site/icn-edit.png" alt="editar" width="32" /></td>
								<td><a href="/admin/delete'. $linkEdit . '/id/' . $value .'" title="borrar"><img src="/images/site/icn-delete.png" alt="borrar" width="32" /></a></td>';
						} else {
							$outputBody .= '<td>'. $value .'</td>';
						}
		        }
		        $outputBody .= '</tr>';
			} else {
				$outputBody .= '<tr><td>' . $bodData . '</td></tr>';
			}
		}
        return $outputBody;			
	}
}